
/*
  @Author: 于辉
  @Time: 2020-11
  @Tittle: dictionary
  @Description: 字典相关
*/
var dataList ,layindex;
layui.use(['layer','jquery','form'],function(){
    var layer = layui.layer,
        form = layui.form;

    dataList = new Vue({
        el:"#menuList",
        data:{
            dataList:[],
            showIndex:-1,
            icon:''
        },
        methods:{
            showChildred:function(index,showIndex){
                this.showIndex = showIndex;
            },
            eidtInfo:function(firstIndex,secondIndex){
                getAllDataList(function(){
                    form.render();
                    var data =  jQuery.extend({}, dataList.dataList[firstIndex]); ;
                    if(secondIndex != -1){
                        data = dataList.dataList[firstIndex].children[secondIndex];
                    }else{
                        $("#parentID option").removeAttr("disabled");
                        $("#parentID option[value='"+data.id+"']").attr("disabled",true);
                    }
                    console.log(data)
                    form.val('userinfo',data );
                    layindex = layer.open({
                        type: 1,
                        title: "字典信息",
                        closeBtn: 1,
                        shadeClose: true,
                        area: ['603px', '500px'],
                        content: $("#detailInfo")
                    })
                });

            },
            deleteItem:function(id){
                layer.confirm('是否要删除该字典项？', {
                    btn: ['删除','取消'] //按钮
                }, function(){
                    ajaxSetting({
                        url:getServerPath("DELETEDICTIONARY"),
                        type:'get',
                        data:{id:id},
                        success:function (res) {
                            getDataList();
                            layer.msg("删除成功!",{icon:6});
                        }
                    })
                }, function(){

                });

            }
        }
    })

    //搜索
    layui.$("#btn_search").on("click", function () {
        getDataList();
    })
    //添加字典
    $("#btn_add").click(function(){
        getAllDataList(function(){
            form.render();
            form.on('select(changeIcon)', function (data) {
                dataList.icon = data.value;
                // app.updateSex(data.value) 当然你可以在这直接触发其他事件
            });
            form.val('userinfo',{hidden:true} );
            layindex = layer.open({
                type: 1,
                title: "字典信息",
                closeBtn: 1,
                shadeClose: true,
                area: ['603px', '500px'],
                content: $("#detailInfo")
            })
        });

    })

    getDataList();

    //获取字典列表
    function getDataList (){
        console.log(JSON.stringify({name:$("#search_input").val()}))
        ajaxSetting({
            url:getServerPath("GETDICTIONARYLIST"),
            type:"get",
            data:{name:$("#search_input").val()},
            success:function(res){
                console.log(res)
                dataList.dataList = res.data;

            }
        })
    }

    //获取字典列表
    function getAllDataList (callback){
        ajaxSetting({
            url:getServerPath("GETDICTIONARYLIST"),
            type:"get",
            data:{name:''},
            success:function(res){
                $("#parentID").empty();
                $("#parentID").append("<option value=\"\">一级字典</option>");
                for (var i = 0; i < dataList.dataList.length; i++) {
                    $('#parentID').append(new Option(res.data[i].name, res.data[i].id));// 下拉里添加元素
                }
                callback();

            }
        })
    }


    //详情页相关方法

    $("#btn_cancle").click(function () {
        layer.close(layindex);
    })
    //保存方法
    form.on('submit(btn_save)', function(data) {
        data = data.field;
        console.log(data)
        data.parentID = data.parentID==''?'-1':data.parentID;
        console.log(data)
        ajaxSetting({
            url:  data.id==null || data.id == ''? getServerPath("CREATEDICTIONARY"):getServerPath("UPDATEDICTIONARY"),
            data:JSON.stringify(data),
            success:function(res){
                getDataList();
                layer.msg("保存成功!",{icon:6});
                setTimeout(function(){
                    layer.close(layindex)
                },200)
            }
        })
        return false;
    });

});

